/**
 * 项目名：phms3
 * 日期：    2019-11-22 16:35:44
 * Copyright (c) 2015- HTSC-版权所有
 */

package com.joysuch.wwyt.core.repository;

import com.joysuch.wwyt.core.entity.BaseMenuFunction;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * BaseBizCodeSeedDao
 */
public interface BaseMenuFunctionDao
        extends JpaRepository<BaseMenuFunction, Long>, QuerydslPredicateExecutor<BaseMenuFunction> {
    public Page<BaseMenuFunction> findAll(Specification<BaseMenuFunction> spec, Pageable pageable);

    public BaseMenuFunction save(BaseMenuFunction bean);

    public void delete(BaseMenuFunction bean);

    // public BaseUser findByCode(String code);

    @Query("select b from BaseMenuFunction b where b.id in (?1) order by menuOrder asc")
    public List<BaseMenuFunction> selectListById(List<Long> ids);

    @Query(value = "select * from wwyt_base_menu_function where depth=?1 and (show_state is null or show_state =1)and delete_flag='0' order by menu_order asc ", nativeQuery = true)
    public List<BaseMenuFunction> findByDepth(Integer depth);

    @Query(value = "select * from wwyt_base_menu_function where id in ?1 and (show_state is null or show_state =1)and delete_flag='0' order by menu_order asc ", nativeQuery = true)
    List<BaseMenuFunction> findByIds(List<Long> ids);

    public BaseMenuFunction findByName(String parentName);

    @Query(value = "select count(*) from wwyt_base_menu_function where name=?1 and parent_id=?2 and delete_flag='0'", nativeQuery = true)
    int countByNameAndParentId(@Param("name") String name, @Param("parentId") Long parentId);

    int countByName(String name);

    @Query("select b from BaseMenuFunction b where b.name=?1")
    List<BaseMenuFunction> selectByName(String name);

    @Query(value = "select * from wwyt_base_menu_function where work_flow_id=?1 and `TYPE` !=?2 and delete_flag='0'", nativeQuery = true)
    List<BaseMenuFunction> findByWorkFlowIdAndExcludeType(Long workFlowId, String type);

    @Query(value = "select * from wwyt_base_menu_function where work_flow_id=?1 and `TYPE` =?2 and delete_flag='0'", nativeQuery = true)
    List<BaseMenuFunction> findByWorkFlowIdAndType(Long workFlowId, String type);

    BaseMenuFunction findFirstByTypeAndName(String type, String name);

    List<BaseMenuFunction> findByParent(BaseMenuFunction parent);

    @Query(value = "select * from wwyt_base_menu_function where (show_state is null or show_state= 1 ) and delete_flag='0' order by menu_order asc ", nativeQuery = true)
    List<BaseMenuFunction> findByShowState();

    @Query(value = "select * from wwyt_base_menu_function where (show_state is null or show_state= 1 ) and  id in(?1) and delete_flag='0' order by menu_order asc ", nativeQuery = true)
    List<BaseMenuFunction> findByIdsAndShowState(List<Long> menuIds);

    @Query(value = "select * from wwyt_base_menu_function where parent_id =?1 and delete_flag='0'", nativeQuery = true)
    List<BaseMenuFunction> findByParentId(Long id);

    BaseMenuFunction findFirstByParentIdAndType(Long id, String type);

    @Query(value = "select count(*) from wwyt_base_menu_function where app_name=?1 and parent_id=?2 and delete_flag='0'", nativeQuery = true)
    int countByAppNameAndParentId(String appName, Long parentId);

    @Query(value = "select count(*) from wwyt_base_menu_function where app_name=?1 and parent_id is null and delete_flag='0'", nativeQuery = true)
    int countByAppNameAndParentIdIsNull(String appName);

    BaseMenuFunction findFirstByNameAndParentAndWorkFlowId(String name, BaseMenuFunction parent, Long workFlowId);

    BaseMenuFunction findFirstByParentId(Long parentId);

    @Query(value = "select * from wwyt_base_menu_function where parent_id is null and delete_flag='0' limit 1", nativeQuery = true)
    BaseMenuFunction findFirstByParentIdIsNull();

    @Query(value = "select * from wwyt_base_menu_function where id in(?1) and `TYPE` in (?2) and app_flag =?3 and delete_flag='0' ", nativeQuery = true)
    List<BaseMenuFunction> findAppMenuByParam(List<Long> menuIds,List<String> types,String appFlag);

    @Query(value = "select * from wwyt_base_menu_function where `TYPE` in (?1) and app_flag =?2 and delete_flag='0' ", nativeQuery = true)
    List<BaseMenuFunction> findAppMenuByParamForAdmin(List<String> types,String appFlag);

    @Query(value = "select * from wwyt_base_menu_function where  delete_flag='0' and show_state='1'", nativeQuery = true)
    List<BaseMenuFunction> findAllByShowState();

    @Modifying
    @Transactional
    @Query(value = "update wwyt_base_menu_function set PARENT_ID = ?1 where ID = ?2 ", nativeQuery = true)
    void updateParentId(Long parentId,Long id);

    @Modifying
    @Transactional
    @Query(value = "update wwyt_base_menu_function set DELETE_FLAG = '1' where ID = ?1", nativeQuery = true)
    void deleteByExMenuId(Long id);
}
